package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.ProfitListAdapter;
import com.eeepay.eeepay_v2.model.ProfitInfo;
import com.eeepay.eeepay_v2.util.BaseGrpcTask;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.log.LogUtils;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.view.TitleBar;

import java.util.ArrayList;
import java.util.List;

import cn.eeepay.api.grpc.nano.ProductApiServiceGrpc;
import cn.eeepay.api.grpc.nano.ProductSerProto;
import cn.eeepay.api.grpc.nano.ReqBaseProto;
import io.grpc.ManagedChannel;

/**
 * Created by zw on 2016/3/14 0014.
 * 分润信息
 */
public class ServeRateActivity extends ABBaseActivity implements AdapterView.OnItemClickListener {

    private TitleBar titleBar;
    private ListView lv_profit;

    private ProfitListAdapter adapter;

    private String agentId;//当前代理商ID

    @Override
    protected int getLayoutId() {
        return R.layout.activity_profit_info;
    }

    @Override
    protected void initWidget() {

        if(bundle != null){
            agentId = bundle.getString(Constant.AGENT_ID);
            LogUtils.d("TAG", "agent_id = " + agentId);
        }
        titleBar = getViewById(R.id.title_bar);
        titleBar.setTiteTextView("服务费率");
        lv_profit = getViewById(R.id.lv_profit_info);
        adapter = new ProfitListAdapter(mContext);
        lv_profit.setAdapter(adapter);

//        adapter.add(new ProfitInfo("超级快", "所有银行卡", "节假日", ""));
//        adapter.add(new ProfitInfo("超级刷", "所有银行卡", "节假日", "100%"));
//        adapter.add(new ProfitInfo("超级猛", "建设银行", "节假日", ""));
//        adapter.add(new ProfitInfo("小宝", "所有银行卡", "工作日", "每笔收益一元"));
//        adapter.add(new ProfitInfo("超级快", "所有银行卡", "节假日", "每笔收益一元"));\

        sendHttpRequest(0);
    }

    @Override
    protected void sendHttpRequest(int taskId) {
        super.sendHttpRequest(taskId);
        new GrpcTask().execute(Constant.URL.URL, Constant.URL.PORT);
    }

    @Override
    protected void eventOnClick() {
        lv_profit.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //add by zhuangzeqin 2017年6月7日10:48:24 暂时屏蔽掉；不需要点击；可能后续需要开发.....
//        Bundle bundle = new Bundle();
//        bundle.putSerializable(Constant.PROFIT_INFO, adapter.getItem(position));
//        goActivity(ProfitSetActivity.class, bundle);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        sendHttpRequest(0);
    }

    private class GrpcTask extends BaseGrpcTask<ProductSerProto.AgentRateApiList> {

        @Override
        protected ProductSerProto.AgentRateApiList doInBack(ManagedChannel channel) {
            ProductApiServiceGrpc.ProductApiServiceBlockingStub stub = ProductApiServiceGrpc.newBlockingStub(channel);
            ReqBaseProto.ReqOneStr request = new ReqBaseProto.ReqOneStr();
            request.param = agentId;
            ProductSerProto.AgentRateApiList response = stub.getAgentRateList(request);
            return response;
        }

        @Override
        protected void updateUI(ProductSerProto.AgentRateApiList result) {
            if(result == null) {
                showToast("查询失败");
                return;
            }
            if(result.resStatus.status){
                ProductSerProto.AgentRateApi[] list = result.rateList;
                if(list.length > 0){
                    List<ProfitInfo> datas = new ArrayList<>();
                    for (int i = 0; i < list.length; i++){
                        ProfitInfo info = new ProfitInfo();
                        info.setId(list[i].id + "");
                        info.setAgentId(list[i].agentNo);
                        info.setServiceId(list[i].serviceId);
                        info.setServiceName(list[i].serviceName);
                        info.setCardType(list[i].cardType);
                        info.setTradeTime(list[i].holidaysMark);
                        info.setInCome(list[i].merRate);
//                        info.setServiceType(list[i].s);
                        info.setFlag("服务费率:");
                        datas.add(info);
                    }
                    adapter.setList(datas);
                }
            }
        }
    }
}
